﻿using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.UI.Notifications;

namespace Sumc.Client.ModernUI.Helpers
{
    public class TileService : ITileService
    {
        private const string TileKey = "this.tileUpdaterUpdate";
        private readonly ApplicationDataContainer settings;
        private readonly TileUpdater tileUpdater;

        public TileService()
        {
            this.settings = ApplicationData.Current.LocalSettings;
            this.tileUpdater = TileUpdateManager.CreateTileUpdaterForApplication();
        }

        public bool IsTileRegistred()
        {
            var value = this.settings.Values[TileKey];
            if (value == null)
            {
                return false;
            }

            var isRegistred = (bool)value;
            return isRegistred;
        }

        public bool RegisterPeriodicUpdate(string url, PeriodicUpdateRecurrence period)
        {
            if (this.IsTileRegistred())
            {
                return false;
            }

            var uri = new Uri(url);
            this.tileUpdater.StartPeriodicUpdate(uri, period);
            this.settings.Values[TileKey] = true;
            return true;
        }

        public bool RegisterPeriodicUpdate(string url, DateTimeOffset startTime, PeriodicUpdateRecurrence period)
        {
            if (this.IsTileRegistred())
            {
                return false;
            }

            var uri = new Uri(url);
            this.tileUpdater.StartPeriodicUpdate(uri, startTime, period);
            this.settings.Values[TileKey] = true;
            return true;
        }

        public bool RegisterPeriodicUpdateBatch(IEnumerable<string> urlList, PeriodicUpdateRecurrence period)
        {
            if (this.IsTileRegistred())
            {
                return false;
            }

            this.tileUpdater.EnableNotificationQueue(true);
            var uriList = this.GetUriList(urlList);
            this.tileUpdater.StartPeriodicUpdateBatch(uriList, period);
            this.settings.Values[TileKey] = true;
            return true;
        }

        public bool RegisterPeriodicUpdateBatch(IEnumerable<string> urlList, DateTimeOffset startTime, PeriodicUpdateRecurrence period)
        {
            if (this.IsTileRegistred())
            {
                return false;
            }

            this.tileUpdater.EnableNotificationQueue(true);
            var uriList = this.GetUriList(urlList);
            this.tileUpdater.StartPeriodicUpdateBatch(uriList, startTime, period);
            this.settings.Values[TileKey] = true;
            return true;
        }

        public bool StopPeriodicUpdate()
        {
            if (!this.IsTileRegistred())
            {
                this.tileUpdater.StopPeriodicUpdate();
                this.settings.Values[TileKey] = false;
                return true;
            }

            return false;
        }

        private IEnumerable<Uri> GetUriList(IEnumerable<string> urlList)
        {
            foreach (var url in urlList)
            {
                var uri = new Uri(url);
                yield return uri;
            }
        }
    }
}
